package com.gray.serverconfig.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * Created on 2019/6/21.
 *
 * @author Grak
 * @since 1.0
 */
@RestController
public class RefreshController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${server.port}")
    private int port;

    @RequestMapping("/refresh")
    public String refresh() {
        String url = String.format("http://%s:%s", "localhost", port) + "/actuator/bus-refresh";
        URI uri = URI.create(url);
        //消息头
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置contentType
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        String httpBody = "{\"motto\":\"配置刷新\"}";
        HttpEntity<String> httpEntity = new HttpEntity<String>(httpBody, httpHeaders);
        ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, httpEntity, String.class);
        return response.getBody();
    }
}
